package com.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.gulimall.product.entity.AttrEntity;
import com.gulimall.product.service.AttrAttrgroupRelationService;
import com.gulimall.product.service.AttrService;
import com.gulimall.product.service.impl.CategoryServiceImpl;
import com.gulimall.product.vo.AttrGroupRelationVo;
import com.gulimall.product.vo.AttrGroupWIthAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gulimall.product.entity.AttrGroupEntity;
import com.gulimall.product.service.AttrGroupService;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.R;


/**
 * 属性分组
 *
 * @author 王半仙
 * @email 1979526040@qq.com
 * @date 2022-11-22 22:14:33
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryServiceImpl categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
//        PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 获取属性分组关联的所有属性
     * * @param attrGroupId
     *
     * @return
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R getAttrGroupRelation(@PathVariable Long attrGroupId) {
        List<AttrEntity> attrEntityList = attrService.getAttrGroupRelationById(attrGroupId);
        return R.ok().put("data", attrEntityList);
    }

    /**
     * 获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
     * @param attrGroupId 属性分组id
     * @param params 分页参数
     * @return
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R getAttrGroupNotRelation(@PathVariable Long attrGroupId,
                                     @RequestParam Map<String, Object> params
    ) {
        PageUtils page = attrService.getAttrGroupNotRelation(attrGroupId, params);
        return R.ok().put("page", page);
    }


    /**
     * 删除分组中关联的属性
     *
     * @param relationIds
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody List<AttrGroupRelationVo> relationIds) {
        attrGroupService.deleteAttrGroupRelations(relationIds);
        return R.ok();
    }

    /**
     * 新增分组与属性关联关系
     * @return
     */
    @PostMapping("/attr/relation")
    public R addAttrRelation(@RequestBody List<AttrAttrgroupRelationEntity> relationEntityList){
        relationService.saveBatch(relationEntityList);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath = categoryService.getCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 获取分类下的所有属性分组和关联的属性
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupAndAttr(@PathVariable Long catelogId){
        List<AttrGroupWIthAttrsVo> list = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",list);
    }

}
